//
//  Primer_String_DemoVC.swift
//  APSwift
//
//  Created by jianbo.pei on 2022/2/18.
//

import UIKit

class Primer_String_DemoVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
//        String.CharacterView
        
//        let point = CGPoint(x: 21, y: 30)
//        print("point is \(point)")
//        let str = String(describing: point)
//        print("pointStr is \(str)")
//
//        let ab = String(repeating: "abcd", count: 4)
//        print("ab is \(ab)")
//
//        let subStr = subStrDemo(str: "@惜沫🥀 你好呀", from: 4)
//        debugPrint("str is [\(subStr)]")
        
//        let mess = "@惜沫🥀 哈哈哈"
//        var spacingIndex = 0
//        for text in mess {
//            if text == " " {
//                break
//            }
//            spacingIndex += 1
//        }
//        let sufIndex: String.Index = mess.index(mess.startIndex, offsetBy: spacingIndex+1)
//        let sufStr: String = String(mess[..<sufIndex])
//        debugPrint("111 - sufIndex is \(sufIndex), sufStr is \(sufStr)")
//
//        let nsstr = NSString(string: mess)
//        let nsrange = nsstr.range(of: sufStr)
//        debugPrint("111 - nsrange is \(nsrange), nsstr is \(nsstr)")
//        debugPrint("111 - str.count is \(mess.count), nsstr.length is \(nsstr.length)")

        test6()
    }
    
    private func test7() {
        
    }
    
    func isPurnInt(string: String) -> Bool {
        
        let scan: Scanner = Scanner(string: string)
        
        var val:Int = 0
        
        return scan.scanInt(&val) && scan.isAtEnd
        
    }
    

    
    private func test6() {
        let agree = "同意"
        let aboutProtocol = "相关协议"
        let txt = agree + aboutProtocol
        let attStr = NSMutableAttributedString(string: txt)
        if let range = txt.range(of: aboutProtocol) {
            let nsrange = NSRange(range, in: txt)
            attStr.addAttribute(.foregroundColor, value: UIColor.red, range: nsrange)
        }
        
        let lb = UILabel()
        lb.frame = CGRect(x: 10, y: 200, width: 300, height: 50)
        view.addSubview(lb)
        lb.backgroundColor = .black
        lb.textColor = .white
        lb.attributedText = attStr
    }
    
    private func test5() {
//        let trueStr = String(true)
        let str = "Greetings, friend! How are you?"
        let firstSpace = str.firstIndex(of: " ") ?? str.endIndex
        let substr = str[firstSpace...]
        if let nextCapital = substr.firstIndex(where: { $0 >= "A" && $0 <= "z" }) {
            print("Capital after a space: \(str[nextCapital])")
        }
    }
    
    private func test4() {
//        let s = "Hello World"
//        let wo = "wo"
//        let range = s.range(of: wo)
//        let location = s.distance(from: s.startIndex, to: range?.lowerBound)
    }

    
    private func test3() {
        let s = "Hello World"
        let hello = s.subString(to: 4)
        debugPrint("hello is \(hello)")
        
        let from1 = s.subString(from: 1)
        debugPrint("from1 is \(from1)")
        let from2 = s.subString(from: -1)
        let from3 = s.subString(from: s.count + 2)
        debugPrint("from2 is \(from2), from3 is \(from3)")
    }
    
    private func test2() {
        let s = "Hello World"
        let r1 = s.range(of: "Hello", options: .caseInsensitive)
        debugPrint("r is \(String(describing: r1))")
        debugPrint("s.endIndex is \(s.endIndex)")
    }
    
    private func test1() {
        let s = "Swift"
        let i = s.index(s.startIndex, offsetBy: -s.count)
        debugPrint("c is \(s[..<i])")
        
//        let i1 = s.index(s.startIndex, offsetBy: 10)
//        let c1 = s[i1]
//        debugPrint("c1 is \(c1)")
        
        //before
       let rain = "The is\"new\"string"
        //after
        let rain1 = #"The is"new"string"#
        
        print("rain is \(rain), rain1 is \(rain1)")
    }
    
}


